package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoClean;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
	
	private final CategoryMapper categoryMapper;
	
	public CategoryServiceImpl(CategoryMapper categoryMapper) {
		this.categoryMapper = categoryMapper;
	}
	
	@Override
	public Result<PageResult> page(CategoryPageQueryDTO pageQueryDTO) {
		if (pageQueryDTO.getPage() == null) {
			pageQueryDTO.setPage(1);
		}
		if (pageQueryDTO.getPageSize() == null) {
			pageQueryDTO.setPageSize(10);
		}
		PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
		List<Category> employeePage = categoryMapper.getCategoryBy(pageQueryDTO.getName(), pageQueryDTO.getType());
		Page<Category> p = (Page<Category>) employeePage;
		PageResult pageResult = new PageResult();
		pageResult.setTotal(p.getTotal());
		pageResult.setRecords(p.getResult());
		return Result.success(pageResult);
	}
	
	@Override
	@AutoClean
	public Result insert(CategoryDTO categoryDTO) {
		if (categoryDTO.getName() == null || categoryDTO.getType() == null || categoryDTO.getSort() == null) {
			return Result.error(MessageConstant.INCORRECT_PARAMETERS);
		}
		List<Category> categoryList = categoryMapper.getCategoryByName(categoryDTO.getName());
		if (!categoryList.isEmpty()) {
			return Result.error(MessageConstant.ITEM_ALREADY_EXISTS);
		}
		Category category = new Category();
		category.setStatus(StatusConstant.ENABLE);
		BeanUtils.copyProperties(categoryDTO, category);
		return Result.success(categoryMapper.insert(category));
	}
	
	@Override
	@AutoClean
	public Result deleteById(Long id) {
		if (id == null) {
			return Result.error(MessageConstant.INCORRECT_PARAMETERS);
		}
		return Result.success(categoryMapper.deleteById(id));
	}
	
	@Override
	@AutoClean
	public Result update(CategoryDTO categoryDTO) {
		if (categoryDTO.getId() == null || categoryDTO.getName() == null || categoryDTO.getSort() == null) {
			return Result.error(MessageConstant.INCORRECT_PARAMETERS);
		}
		Category category = new Category();
		BeanUtils.copyProperties(categoryDTO, category);
		categoryMapper.update(category);
		return Result.success();
	}
	
	@Override
	@AutoClean
	public Result banCategory(Integer status, Long id) {
		if (status == null || id == null) {
			return Result.error(MessageConstant.INCORRECT_PARAMETERS);
		}
		Category category = categoryMapper.getCategoryBy(null, null, id).remove(0);
		category.setStatus(status);
		categoryMapper.update(category);
		return Result.success();
	}
	
	@Override
	@Cacheable(cacheNames = "CategoryCache", key = "#type")
	public Result getListByType(Integer type) {
		return Result.success(categoryMapper.getCategoryBy(null, type));
	}
}
